﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Marmat.Forms.Views.DataGrid;

namespace Marmat.Forms.Design
{
    public partial class ColumnCollectionForm : CollectionEditorForm<Marmat.Forms.Views.DataGrid.Column>
    {
        private Marmat.Forms.DataGrid _dataGrid;
        public ColumnCollectionForm(Marmat.Forms.DataGrid dataGrid) : base(dataGrid.Columns)
        {
            InitializeComponent();
            this._dataGrid = dataGrid;

            if (dataGrid is Marmat.Forms.Local.DataGrid grid)
            {
                if (string.IsNullOrEmpty(grid.ConnectionName) || string.IsNullOrEmpty(grid.LoadTableName))
                    this.bButton1.Visible = false;
                else
                    this.bButton1.Visible = true;
            }
            else
                this.bButton1.Visible = false;
        }

        /// <summary>
        /// 添加新项时执行。
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        /// <returns></returns>
        protected override Column NewItem(object sender, EventArgs e)
        {
            int count = this._collection.Count;
            do
            {
                count++;
                string columnName = $"第{count}列";
                if (this._collection.FirstOrDefault(x => x.Name == columnName) is null)
                {
                    Column column = new Column();
                    column.IsTemporary = true;
                    column.Name = columnName;
                    column.DataType = typeof(string);
                    return column;
                }
            } while (true);
        }


        /// <summary>
        /// 导入数据字段
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void BButton1_Click(object sender, EventArgs e)
        {
            if (this._dataGrid is Marmat.Forms.Local.DataGrid dataGrid)
            {
                DataColumnForm form = new DataColumnForm(dataGrid, this._collection);
                if (form.ShowDialog() == DialogResult.OK)
                {
                    bool defalutCaption = form.BCheckBox1.Checked;
                    foreach (Marmat.Forms.Views.DataGrid.Row row in form.BGrid1.Rows)
                    {

                        if (row["选择"].Equals(true))
                        {
                            Marmat.Forms.Views.DataGrid.Column column;
                            if (this._collection.Exists(x => x.Name == row["列名"].ToString()))
                                column = this._collection.FirstOrDefault(x => x.Name == row["列名"].ToString());
                            else
                            {
                                column = new Column();
                                column.Name = row["列名"].ToString();
                                if (defalutCaption)
                                    column.Caption = row["字段描述"].ToString();
                                column.Visible = !Convert.ToBoolean(row["是否主键"]);
                            }
                            column.DataType = row["数据类型"] as Type;
                            column.MaxLength = Convert.ToInt32(row["长度"]);
                            column.PrimaryKey = Convert.ToBoolean(row["是否主键"]);
                            if (!this._collection.Exists(x => x.Name == row["列名"].ToString()))
                                this.AddItem(column);
                        }
                    }
                }
            }
        }
    }
}
